import json
from datetime import date
from datetime import datetime


class JsonCustomEncoder(json.JSONEncoder):

    def default(self, field):

        if isinstance(field, datetime):
            return field.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(field, date):
            return field.strftime('%Y-%m-%d')
        else:
            return json.JSONEncoder.default(self, field)


"""
处理时间数据类型
# # 测试查询
# def find(req):
#     # 查询数据库数据
#     ret_data = User.objects.filter(id=66).values('user_level', 'login_time').first()
#     # 判断是否存在数据
#     if ret_data:
#         # 列表化对象
#         ret = list(ret_data)
#         # 自定义返回数据
#         data = {
#             'status': 200,
#             'msg': ret
#         }
#         # 序列化，JsonCustomEncoder为自定义日期处理类
#         res = json.dumps(data, cls=JsonCustomEncoder, ensure_ascii=False)
#         return HttpResponse(res, content_type='application/json')
#     data = {
#         'static': 200,
#         'msg': None
#     }
#     return JsonResponse(data)
"""
